package cn.jstars.datatocloud.common.utils;

import java.util.List;

import com.google.common.collect.Lists;

/**
 * 分页工具类
 * 
 * @author Elvish
 *
 */
public class PageUtil {

	/**
	 * limit list by page
	 * 
	 * @param list
	 *            data list
	 * @param pageSize
	 *            page Size
	 * @return limit list
	 * @return limit object
	 */
	public static <T> List<List<T>> paging(List<T> list, int pageSize) {
		int totalCount = list.size();
		int pageCount;
		int m = totalCount % pageSize;

		if (m > 0) {
			pageCount = totalCount / pageSize + 1;
		} else {
			pageCount = totalCount / pageSize;
		}

		List<List<T>> totalList = Lists.newArrayList();
		for (int i = 1; i <= pageCount; i++) {
			if (m == 0) {
				List<T> subList = list.subList((i - 1) * pageSize, pageSize * (i));
				totalList.add(subList);
			} else {
				if (i == pageCount) {
					List<T> subList = list.subList((i - 1) * pageSize, totalCount);
					totalList.add(subList);
				} else {
					List<T> subList = list.subList((i - 1) * pageSize, pageSize * i);
					totalList.add(subList);
				}
			}
		}

		return totalList;
	}

}
